﻿package com
{
	import com.mathMl.MathPanel;
	import com.mathMl.MathPanelEvent;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author Mohan Patil
	 */
	public class AdvDropDown extends MovieClip
	{
		private var _items:Array = new Array();
		private var _selectedItem:AdvDropDownItem;
		private var arrDone:Array = new Array();
		private var _dropDownWidth:Number;
		private var _fontFamily:String;
		private var _loaderInfoObj:LoaderInfo;
		private var _fontSize:Number;
		private var _textFormat:TextFormat;
		private var _correctItemId;
		private var _correctItem;
		private var _shuffle;
		private var _locked:Boolean
		private var mcItems:MovieClip;
		
		//public function AdvDropDown(fontFamily:String = "", fontSize:Number = 0, loaderInfoObj:LoaderInfo = null, dropDownWidth:Number = 0)
		public function AdvDropDown(textFormat:TextFormat, dropDownWidth:Number = 0, shuffle:Boolean = false)
		{
			//_fontFamily = fontFamily;
			_textFormat = textFormat;
			_textFormat.leading = 5;
			//_fontSize = fontSize;
			//_loaderInfoObj = loaderInfoObj
			_dropDownWidth = dropDownWidth;
			_shuffle = shuffle;
			mcBg.width = _dropDownWidth;
			mcDrop.width = _dropDownWidth;
			mcArrow.x = mcDrop.x + mcDrop.width - 10.5;
			mcSelectedItem.txtContent.width = _dropDownWidth - 10;
			mcSelectedItem.mcBg.width = _dropDownWidth;
			mcSelectedItem.mcBg.height = mcSelectedItem.txtContent.textHeight + 8;
			mcSelectedItem.mcPatch.width = mcSelectedItem.mcBg.width;
			mcSelectedItem.mcPatch.height = mcSelectedItem.mcBg.height;
			mcSelectedItem.y = 0 - mcSelectedItem.height / 2; //Added for test only
			mcBg.y = mcSelectedItem.y; //Added for Test only.
			mcBg.height = mcSelectedItem.height; //Added for Test only.
			mcDrop.y = mcSelectedItem.y;
			mcDrop.height = mcBg.height;
			mcArrow.y = mcDrop.y + mcDrop.height / 2;
			mcArrow.mcLine.height = mcDrop.height - 2;
			mcDrop.mouseEnabled = true;
			mcDrop.buttonMode = true;
			mcDrop.addEventListener(MouseEvent.CLICK, mcDrop_Click);
			mcItems = new AdvDropDownItemsContainer();
			mcItems.name = "mcItems";
			mcItems.visible = false;
			mcItems.bg.width = _dropDownWidth;
			mcSelectedItem.addEventListener(AdvDropDownEvent.ITEM_ADDED, selectedItemAdded);
		}
		
		private function mcDrop_Click(e:MouseEvent):void
		{
			if (this.getChildByName("mcItems") == null)
			{
				//trace("this.getChildByName(\"mcItems\") == null");
				this.addChild(mcItems);
				mcItems.visible = true;
				this.dispatchEvent(new AdvDropDownEvent(AdvDropDownEvent.ITEM_LIST_SHOWN));
				try
				{
					this.parent.setChildIndex(this, this.parent.numChildren - 1);
				}
				catch(er:Error)
				{
					
				}
			}
			else
			{
				//trace("this.getChildByName(\"mcItems\") != null");
				mcItems.visible = false;
				this.removeChild(mcItems);
			}
			/*if (mcItems.visible)
			{
				mcItems.visible = false;
			}
			else
			{
				mcItems.visible = true;
			}*/
		}
		
		public function setSelectedItemById(itemId:Number)
		{
			if (items.length > 0 && itemId <= items.length - 1)
			{
				var tmpItem:AdvDropDownItem = getItemById(itemId);
				//trace(e.currentTarget.name);
				//mcItems.visible = false;
				AdvDropDownItem(mcSelectedItem).id = tmpItem.id;
				AdvDropDownItem(mcSelectedItem).type = tmpItem.type;
				AdvDropDownItem(mcSelectedItem).data = tmpItem.data;
				//AdvDropDownItem(mcSelectedItem).renderItem(_fontFamily, _fontSize, _loaderInfoObj, _dropDownWidth);
				AdvDropDownItem(mcSelectedItem).renderItem(_textFormat, _dropDownWidth);
			}
		}
		
		public function get selectedItem():AdvDropDownItem
		{
			return _selectedItem;
		}
		
		/*public function set selectedItem(value:AdvDropDownItem):void
		{
		   _selectedItem = value;
		}*/
		 
		public function get selectedItemId():Number
		{
			return _selectedItem.id;
		}
		
		public function set selectedItemId(value:Number)
		{
			
		}
		
		public function get items():Array
		{
			return _items;
		}
		
		public function set items(value:Array):void
		{
			_items = value;
		}
		
		public function get correctItemId():Number
		{
			return _correctItemId;
		}
		
		public function set correctItemId(value:Number):void 
		{
			_correctItemId = value;
		}
		
		public function get locked():Boolean 
		{
			return _locked;
		}
		
		public function set locked(value:Boolean):void 
		{
			_locked = value;
			if (_locked)
			{
				mcDrop.mouseEnabled = false;
			}
			else
			{
				mcDrop.mouseEnabled = true;
			}
		}
		
		public function showHighlight()
		{
			if (AdvDropDownItem(mcSelectedItem).id == _correctItemId)
			{
				AdvDropDownItem(mcSelectedItem).mcBg.gotoAndStop("correct");
			}
			else
			{
				AdvDropDownItem(mcSelectedItem).mcBg.gotoAndStop("incorrect");
			}
		}
		
		public function hideHighlight()
		{
			AdvDropDownItem(mcSelectedItem).mcBg.gotoAndStop("normal");
		}
		
		public function showCorrectAnswer()
		{
			var prevCorrect:Boolean;
			if (AdvDropDownItem(mcSelectedItem).id == _correctItemId)
			{
				prevCorrect = true;
			}
			else
			{
				prevCorrect = false;
			}
			AdvDropDownItem(mcSelectedItem).id = _correctItem.id;
			AdvDropDownItem(mcSelectedItem).type = _correctItem.type;
			AdvDropDownItem(mcSelectedItem).data = _correctItem.data;
			AdvDropDownItem(mcSelectedItem).renderItem(_textFormat, _dropDownWidth);
			
			if (prevCorrect)
			{
				AdvDropDownItem(mcSelectedItem).mcBg.gotoAndStop("correct");
			}
			else
			{
				AdvDropDownItem(mcSelectedItem).mcBg.gotoAndStop("incorrect");
			}
		}
		
		public function createItems():void
		{
			for (var i = 0; i < _items.length; i++)
			{
				var tmpItem:AdvDropDownItem = AdvDropDownItem(_items[i]);
				tmpItem.name = "mcItem_" + tmpItem.id;
				tmpItem.addEventListener(AdvDropDownEvent.ITEM_ADDED, itemAdded);
				tmpItem.renderItem(_textFormat, _dropDownWidth);
				mcItems.addChild(tmpItem);
				if (tmpItem.id == _correctItemId)
				{
					_correctItem = tmpItem;
				}
				//trace("Item Added");
			}
		}
		
		private function itemAdded(e:AdvDropDownEvent):void
		{
			//trace("Item Added " + e.currentTarget.name);
			if (checkAllItemsAdded())
			{
				arrangeItems();
			}
		}
		
		private function selectedItemAdded(e:AdvDropDownEvent):void
		{
			//trace("Selected Item Updated. ID = " + e.currentTarget.id);
			mcSelectedItem.y = 0 - mcSelectedItem.mcPatch.height / 2; //Added for test only
			mcDrop.y = mcSelectedItem.y; //Added for test only
			mcBg.y = mcSelectedItem.y; //Added for test only
			mcBg.height = mcSelectedItem.mcPatch.height; //Added for Test only.
			mcItems.y = mcSelectedItem.y + mcSelectedItem.mcPatch.height;
			mcDrop.height = mcSelectedItem.mcPatch.height;
			mcArrow.y = mcDrop.y + mcDrop.height / 2;
			mcArrow.mcLine.height = mcDrop.height - 2;
			_selectedItem = mcSelectedItem;
			dispatchEvent(new AdvDropDownEvent(AdvDropDownEvent.SELECTION_CHANGED, false, false));
		}
		
		private function checkAllItemsAdded():Boolean
		{
			var allAdded:Boolean = true;
			for (var i = 0; i < _items.length; i++)
			{
				if (AdvDropDownItem(_items[i]).addedToItemList == false)
				{
					allAdded = false;
					break;
				}
			}
			return (allAdded);
		}
		
		private function arrangeItems()
		{
			var dy = 0;
			var i:Number;
			
			var indexArr:Array = new Array();
			
			for (i = 0; i < _items.length; i++)
			{
				indexArr.push(i);
			}
			
			if (_shuffle)
			{
				indexArr = randomizeArray(indexArr);
			}
			
			for (i = 0; i < indexArr.length; i++)
			{
				var tmpItem:AdvDropDownItem = AdvDropDownItem(_items[indexArr[i]]);
				tmpItem.buttonMode = true;
				tmpItem.mouseEnabled = true;
				tmpItem.addEventListener(MouseEvent.CLICK, itemClicked);
				tmpItem.x = 0;
				tmpItem.y = dy;
				dy += tmpItem.mcPatch.height;
			}
			mcItems.bg.height = dy;
			mcItems.y = mcSelectedItem.y + mcSelectedItem.mcPatch.height;
			//trace("advDropDown_DrawComplete");
			dispatchEvent(new AdvDropDownEvent(AdvDropDownEvent.DRAW_COMPLETE, false, false));
		}
		
		private function itemClicked(e:MouseEvent):void
		{
			var tmpItem:AdvDropDownItem = AdvDropDownItem(e.currentTarget);
			//trace(e.currentTarget.name);
			//mcItems.visible = false;
			mcItems.visible = false;
			try
			{
				this.removeChild(mcItems);
			}
			catch(er:Error)
			{
				
			}
			AdvDropDownItem(mcSelectedItem).id = tmpItem.id;
			AdvDropDownItem(mcSelectedItem).type = tmpItem.type;
			AdvDropDownItem(mcSelectedItem).data = tmpItem.data;
			AdvDropDownItem(mcSelectedItem).renderItem(_textFormat, _dropDownWidth);
		}
		
		public function getItemById(id:Number):AdvDropDownItem
		{
			var tmpItem:AdvDropDownItem = AdvDropDownItem(_items[id]);
			return (tmpItem);
		}
		
		public function addItem(newItem:AdvDropDownItem)
		{
			//trace("dd : " + this.name + " options : " + newItem.data);
			_items.push(newItem);
		}
		
		private function randomMinMax( min:Number, max:Number ):Number
		{
			return (Math.round(min + (max - min) * Math.random()));
		}
		
		private function randomizeArray(arr:Array):Array
		{
			for (var i:Number = 0; i < arr.length; i++)
			{
			   var rand:uint = int(Math.random() * arr.length);
			   arr.push( arr.splice( rand, 1 )[0] );
			}
			return(arr);
		}
	}
}